/*
Modified WOO Camera by Mondkalb (Modified by MrCash2009)

Contact me (Mondkalb) via E-Mail:
CEO@mondkalb.org
*/

MBG_WOO_FUNC_DetectKeys =
{
	private ["_dir","_pos"];
	MBG_WOO_ControlsSHIFT = ((_this) select 2);
	MBG_WOO_ControlsCONTROL = ((_this) select 3);
	MBG_WOO_ControlsALT = ((_this) select 4);
	MBG_WOO_ControlsRETURN = ( ((_this) select 1) == 14);
	MBG_WOO_ControlsSPACEBAR = ( ((_this) select 1) == 57);
	MBG_WOO_Controls_M = ( ((_this) select 1) == 50);
	MBG_WOO_Controls_N = ( ((_this) select 1) == 49);
	
	
	if (MBG_WOO_Controls_M) then {
		MBG_WOO_OpenMap = true;
		call MBG_WOO_SHOW_OVERVIEWMAP;
	};
	
	if (MBG_WOO_Controls_N) then {
		if (MBG_WOO_nvg) then {
			MBG_WOO_nvg = false;
			camNVG = false;
			camUseNVG false;
		} else {
			MBG_WOO_nvg = true;
			camNVG = true;
			camUseNVG true;
		};
	};
	
	if (MBG_WOO_ControlsSPACEBAR) then {
	
		call MBG_WOO_FUNC_JUMP_TO_SEL_GROUP;
	};
	
	if (MBG_WOO_ControlsRETURN) then {
	
			
			MBG_WOO_CamDist = MBG_WOO_CamDist;
			MBG_WOO_CamHeight = MBG_WOO_CamHeight;
			if (getdir MBG_WOO_Main_cam_Center == 0) then {
				if ((MBG_WOO_CamHeight == MBG_WOO_CamHeightDEF) and (MBG_WOO_CamDist != MBG_WOO_CamDistDEF)) then {
					MBG_WOO_CamDist = MBG_WOO_CamDistDEF;
				};
				
				if (MBG_WOO_CamHeight != MBG_WOO_CamHeightDEF) then {
					MBG_WOO_CamHeight = MBG_WOO_CamHeightDEF;
				};
			} else {
				MBG_WOO_Main_cam_Center setdir 0;
			};
			
		MBG_WOO_Main_cam_View_POS = [(((getpos MBG_WOO_Main_cam_Center) select 0) + ((MBG_WOO_CamDist)*sin 0)),(((getpos MBG_WOO_Main_cam_Center) select 1) + (MBG_WOO_CamDist)*cos 0),MBG_WOO_CamHeight];
	
		MBG_WOO_Main_cam CamSetPos MBG_WOO_Main_cam_View_POS;
		MBG_WOO_Main_cam CamCommit 0.5;
	};
};

MBG_WOO_FUNC_DisableKeys =
{
	MBG_WOO_ControlsSHIFT = false;
	MBG_WOO_ControlsCONTROL = false;
	MBG_WOO_ControlsALT = false;
};
	
MBG_WOO_FUNC_RotateScreen =
{
	private ["_xMovement","_yMovement","_Dir"];

		if (MBG_WOO_ControlsALT) then {
		_xMovement = _this select 1;
		_yMovement = _this select 2;
			
		MBG_WOO_Main_cam_Center setdir ((getdir MBG_WOO_Main_cam_Center) + (_xMovement));
		
		MBG_WOO_CamHeight = MBG_WOO_CamHeight + (_yMovement * 2);
		_dir = getdir MBG_WOO_Main_cam_Center;
		MBG_WOO_Main_cam_View_POS = [(((getpos MBG_WOO_Main_cam_Center) select 0) + ((MBG_WOO_CamDist)*sin _dir)),(((getpos MBG_WOO_Main_cam_Center) select 1) + (MBG_WOO_CamDist)*cos _dir),MBG_WOO_CamHeight];

		MBG_WOO_Main_cam CamSetPos MBG_WOO_Main_cam_View_POS;
		MBG_WOO_Main_cam CamCommit 0.0;

	};
};

MBG_WOO_FUNC_MoveScreen =
{
	private ["_xCoord","_yCoord","_thresholdX","_thresholdY","_lastpos","_newDir","_dir","_scrllUpZone","_scrllDownZone","_scrllLeftZone","_scrllRightZone"];
	
		if (MBG_WOO_RMB) then {
			
			if (MBG_WOO_CREATING_NEW_WP) then {
			_lastpos = screenToWorld [_this select 1,_this select 2];
			_newDir = ((_lastpos select 0) - ((getpos MBG_WOO_NEW_WAYPOINT_INDICATOR) select 0)) atan2 ((_lastpos select 1) - ((getpos MBG_WOO_NEW_WAYPOINT_INDICATOR) select 1));
			_newDir = _newDir % 360;
			MBG_WOO_NEW_WAYPOINT_INDICATOR setdir _newDir;
			};
	} else {
		//Move Screen
		if (not MBG_WOO_ControlsALT) then {
			_xCoord = (_this select 1);
			_yCoord = (_this select 2);
				
			_thresholdX = abs (safeZoneX * 0.85);
			_thresholdY = abs (safeZoneY * 0.85);

			_scrllUpZone = (0 - _thresholdy);
			_scrllDownZone = (1 + _thresholdy);
			_scrllLeftZone = (0 - _thresholdX);
			_scrllRightZone = (1 + _thresholdX);
			
			if (_thresholdy == 0) then {
				_scrllUpZone = 0.05;
				_scrllDownZone = 0.95;
			};
			
			if (_thresholdX == 0) then {
				_scrllLeftZone = 0.05;
				_scrllRightZone = 0.95;
			};

			if (_xCoord > _scrllRightZone) then {
			
				_dir = (getdir MBG_WOO_Main_cam_Center) + 90;
				MBG_WOO_Main_cam_Center_POS = [(((getpos MBG_WOO_Main_cam_Center) select 0) + ((1)*sin _dir)),(((getpos MBG_WOO_Main_cam_Center) select 1) + (1)*cos _dir), ((getpos MBG_WOO_Main_cam_Center) select 2)];
				MBG_WOO_Main_cam_Center setpos MBG_WOO_Main_cam_Center_POS;
				_dir = getdir MBG_WOO_Main_cam_Center;
				MBG_WOO_Main_cam_View_POS = [(((getpos MBG_WOO_Main_cam_Center) select 0) + ((MBG_WOO_CamDist)*sin _dir)),(((getpos MBG_WOO_Main_cam_Center) select 1) + (MBG_WOO_CamDist)*cos _dir),MBG_WOO_CamHeight];
			};
				
			if (_xCoord < _scrllLeftZone ) then {
				_dir = (getdir MBG_WOO_Main_cam_Center) -90;
				MBG_WOO_Main_cam_Center_POS = [(((getpos MBG_WOO_Main_cam_Center) select 0) + ((1)*sin _dir)),(((getpos MBG_WOO_Main_cam_Center) select 1) + (1)*cos _dir), ((getpos MBG_WOO_Main_cam_Center) select 2)];
				MBG_WOO_Main_cam_Center setpos MBG_WOO_Main_cam_Center_POS;
				_dir = getdir MBG_WOO_Main_cam_Center;
				MBG_WOO_Main_cam_View_POS = [(((getpos MBG_WOO_Main_cam_Center) select 0) + ((MBG_WOO_CamDist)*sin _dir)),(((getpos MBG_WOO_Main_cam_Center) select 1) + (MBG_WOO_CamDist)*cos _dir),MBG_WOO_CamHeight];

			};
				
			if (_yCoord > _scrllDownZone) then {
				_dir = (getdir MBG_WOO_Main_cam_Center) - 180;
				MBG_WOO_Main_cam_Center_POS = [(((getpos MBG_WOO_Main_cam_Center) select 0) + ((1)*sin _dir)),(((getpos MBG_WOO_Main_cam_Center) select 1) + (1)*cos _dir), ((getpos MBG_WOO_Main_cam_Center) select 2)];
				MBG_WOO_Main_cam_Center setpos MBG_WOO_Main_cam_Center_POS;
				_dir = getdir MBG_WOO_Main_cam_Center;
				MBG_WOO_Main_cam_View_POS = [(((getpos MBG_WOO_Main_cam_Center) select 0) + ((MBG_WOO_CamDist)*sin _dir)),(((getpos MBG_WOO_Main_cam_Center) select 1) + (MBG_WOO_CamDist)*cos _dir),MBG_WOO_CamHeight];

			};
				
			if (_yCoord < _scrllUpZone) then {
				_dir = (getdir MBG_WOO_Main_cam_Center);
				MBG_WOO_Main_cam_Center_POS = [(((getpos MBG_WOO_Main_cam_Center) select 0) + ((1)*sin _dir)),(((getpos MBG_WOO_Main_cam_Center) select 1) + (1)*cos _dir), ((getpos MBG_WOO_Main_cam_Center) select 2)];
				MBG_WOO_Main_cam_Center setpos MBG_WOO_Main_cam_Center_POS;
				_dir = getdir MBG_WOO_Main_cam_Center;
				MBG_WOO_Main_cam_View_POS = [(((getpos MBG_WOO_Main_cam_Center) select 0) + ((MBG_WOO_CamDist)*sin _dir)),(((getpos MBG_WOO_Main_cam_Center) select 1) + (MBG_WOO_CamDist)*cos _dir),MBG_WOO_CamHeight];
			};
			
			MBG_WOO_Main_cam CamSetPos MBG_WOO_Main_cam_View_POS;
			MBG_WOO_Main_cam CamSetTarget MBG_WOO_Main_cam_Center_POS;
			MBG_WOO_Main_cam CamCommit 0.2;
		};
		
	};
};

MBG_WOO_FUNC_ZoomChange =
{
	private ["_DeltaZoomChange","_dir"];
	_DeltaZoomChange = (_this select 1);
	MBG_WOO_Zoomlevel = MBG_WOO_Zoomlevel + ((_DeltaZoomChange/100)*(-1));

	MBG_WOO_CamDist = MBG_WOO_CamDist - ((_DeltaZoomChange*(-1))* 0.4);
	If (MBG_WOO_CamDist > (-1)) then {MBG_WOO_CamDist = (-1)};
	
	_dir = getdir MBG_WOO_Main_cam_Center;
	MBG_WOO_Main_cam_View_POS = [(((getpos MBG_WOO_Main_cam_Center) select 0) + ((MBG_WOO_CamDist)*sin _dir)),(((getpos MBG_WOO_Main_cam_Center) select 1) + (MBG_WOO_CamDist)*cos _dir),MBG_WOO_CamHeight];

	MBG_WOO_Main_cam CamSetPos MBG_WOO_Main_cam_View_POS;
	MBG_WOO_Main_cam CamCommit 0.0;
};

MBG_WOO_FUNC_MouseDown =
{
	private ["_xCoord","_yCoord","_wopos","_selectablesINF","_selectablesVEH","_indicator","_i","_searching","_indica","_bpos","_selectables2","_sideVeh","_meh","_enemyBuildings"];	
	_xCoord = (_this select 2);
	_yCoord = (_this select 3);	
	_wopos = screenToWorld [_xCoord,_yCoord];
	_selectablesINF = nearestObjects [_wopos, ["Man"], 2];
	_selectablesVEH = nearestObjects [_wopos, ["Car","Tank"], 5];
	_selectablesBLD = [];
	_enemyBuildings = [];
	if (side player == west) then {
		_selectablesBLD = nearestObjects [_wopos, MBG_WOO_WEST_ALL_BUILDINGS_TYPES, 4];
		_enemyBuildings = nearestObjects [_wopos, MBG_WOO_EAST_ALL_BUILDINGS_TYPES, 20];
	} else {
		_selectablesBLD = nearestObjects [_wopos, MBG_WOO_EAST_ALL_BUILDINGS_TYPES, 4];
		_enemyBuildings = nearestObjects [_wopos, MBG_WOO_WEST_ALL_BUILDINGS_TYPES, 20];
	};
	_selectables = nearestObjects [_wopos, ["man","Car","Tank"], 5];
	
	//LMB
	if ((_this select 1) == 0) then {
	MBG_WOO_LMB = TRUE;
	

		if ((count _selectablesVEH) > 0) then {[_selectablesVEH, "VEH"] call MBG_WOO_FUNC_SelectUnits;} else {
			if ((count _selectablesINF) > 0) then {[_selectablesINF, "INF"] call MBG_WOO_FUNC_SelectUnits;} else {
				if ((count _selectablesBLD) > 0) then {[_selectablesBLD, "BLD"] call MBG_WOO_FUNC_SelectBuildings;} else {
					if (not (MBG_WOO_TacticalSupport_Selecting)) then {
						call MBG_WOO_FUNC_DeSelectUnits;
						};
					};
				};
			};
	};
};


MBG_WOO_FUNC_MouseUP =
{
	if ((_this select 1) == 1) then {
		MBG_WOO_RMB = FALSE;
		MBG_WOO_CREATING_NEW_WP = FALSE;
	};
	if ((_this select 1) == 0) then {
		MBG_WOO_LMB = false;
			if (MBG_WOO_TacticalSupport_Selecting) then {
			MBG_WOO_TacticalSupport_Selecting = false;
			if (MBG_WOO_TacticalSupport_Type == 0) then {
				[MBG_WOO_TacticalSupport_Marker,1000] call MBG_WOO_FUNC_105_Arty_Cast;
			};
			if (MBG_WOO_TacticalSupport_Type == 1) then {
				[MBG_WOO_TacticalSupport_Marker,1500] call MBG_WOO_FUNC_105_Rocket_Cast;
			};
		};
	};
};

MBG_WOO_FUNC_EmptyPanelMenu =
{
	private ["_control","_x","_y","_w","_h"];
	disableSerialization;
	{CtrlSetText [_x,""];} foreach [59811,59812,59813,59814,59815,59816,59817,59818,59819,598191,598192,598193];
	{ctrlEnable [_x, FALSE];} foreach [59801,59802,59803,59804,59805,59806,59807,59808,59809,598091,598092,598093];
 	_control = (findDisplay 5990) displayCtrl 59880;		
	_control ctrlSetBackgroundColor [0, 0, 0, 0];
	ctrlSetText [59881, ""];
	call MBG_WOO_FUNC_UpdatePanelFunds;
	{
		_control = (findDisplay 5990) displayCtrl _x;		
		_control ctrlSetBackgroundColor [0,0, 0, 0];
		_control ctrlSetTextColor [0,0, 0, 0];
		_control ctrlCommit 0.0;
	} foreach [592,593,59881];
	
	_control = (findDisplay 5990) displayCtrl 59000;
	_x = safeZoneX+(safeZoneW*0.85);
	_y = safeZoneY+(safeZoneH*0.00);
	_w = safeZoneW*0.15;
	_h = safeZoneH*0.10;	
	_control ctrlSetPosition [_x, _y, _w, _h];
	_control ctrlCommit 0.2;
};

MBG_WOO_FUNC_DetectKeysOnMap =
{
	MBG_WOO_Controls_M = ( ((_this) select 1) == 50);
	if (MBG_WOO_Controls_M) then {
		closeDialog 5991;
		if (side player == west) then {
			{clearGroupIcons _x} foreach MBG_WOO_GLOBAL_TOTAL_GROUPS_WEST;
		} else {
			{clearGroupIcons _x} foreach MBG_WOO_GLOBAL_TOTAL_GROUPS_EAST;
		};
		
		deleteMarkerLocal "MBG_WOO_CAMERA_MARKER";
	};
};

MBG_WOO_SHOW_OVERVIEWMAP =
{
	private ["_oo","_map","_te"];
	_oo = createdialog "MBG_WOO_mainMap";
	_map = (findDisplay 5991) displayCtrl 598002;				
	_map ctrlMapAnimAdd [0, 0.1, MBG_WOO_Main_cam_Center_POS];
	ctrlMapAnimCommit _map;
	_display = findDisplay 5991;
	_display displaySetEventHandler ["KeyDown","_this call MBG_WOO_FUNC_DetectKeysOnMap"];
	ctrlSetFocus (findDisplay 5991 displayCtrl 598002);	
	_te = createMarkerLocal ["MBG_WOO_CAMERA_MARKER", MBG_WOO_Main_cam_Center_POS];
	_te setMarkerShapeLocal "ICON";
	"MBG_WOO_CAMERA_MARKER" setMarkerTypeLocal "mil_Arrow2";
	"MBG_WOO_CAMERA_MARKER" setMarkerDirLocal (getdir MBG_WOO_Main_cam_Center);
	"MBG_WOO_CAMERA_MARKER" setMarkerSizeLocal [0.5, 1];
	ctrlMapAnimCommit (findDisplay 5101 displayCtrl 0);
};

MBG_WOO_Overviewmap_SetCamera =
{
	private ["_x","_y","_pos","_dir","_map"];
	_x = _this select 2;
	_y = _this select 3;
	_map = (findDisplay 5991) displayCtrl 598002;	
	_pos = _map ctrlMapScreenToWorld [_x, _y];

	_dir = (getdir MBG_WOO_Main_cam_Center);
	MBG_WOO_Main_cam_Center_POS = _pos;
	MBG_WOO_Main_cam_Center setpos MBG_WOO_Main_cam_Center_POS;
	MBG_WOO_Main_cam_View_POS = [(((getpos MBG_WOO_Main_cam_Center) select 0) + ((MBG_WOO_CamDist)*sin _dir)),(((getpos MBG_WOO_Main_cam_Center) select 1) + (MBG_WOO_CamDist)*cos _dir),MBG_WOO_CamHeight];
	MBG_WOO_Main_cam camPreparePos MBG_WOO_Main_cam_View_POS;
	MBG_WOO_Main_cam CamPrepareTarget MBG_WOO_Main_cam_Center_POS;
	MBG_WOO_Main_cam camCommitPrepared 1;

	_map ctrlMapAnimAdd [0.2, 0.1, _pos];
	ctrlMapAnimCommit _map;	
	"MBG_WOO_CAMERA_MARKER" setMarkerPosLocal _pos;

};

MBG_WOO_FUNC_JUMP_TO_SEL_GROUP =
{

	private ["_dir","_map","_grp","_nmbr"];
	_dir = (getdir MBG_WOO_Main_cam_Center);
	MBG_WOO_Main_cam_Center_POS = (getpos player);
	MBG_WOO_Main_cam_Center setpos MBG_WOO_Main_cam_Center_POS;
	MBG_WOO_Main_cam_View_POS = [(((getpos MBG_WOO_Main_cam_Center) select 0) + ((MBG_WOO_CamDist)*sin _dir)),(((getpos MBG_WOO_Main_cam_Center) select 1) + (MBG_WOO_CamDist)*cos _dir),MBG_WOO_CamHeight];
	MBG_WOO_Main_cam camPreparePos MBG_WOO_Main_cam_View_POS;
	MBG_WOO_Main_cam CamPrepareTarget MBG_WOO_Main_cam_Center_POS;
	MBG_WOO_Main_cam camCommitPrepared 1;
};

MBG_WOO_Overviewmap_MouseButtons =
{
	//RMB	
	if ((_this select 1) == 1) then {
		if (MBG_WOO_CURRENT_SELECTED_GROUP != (group player)) then {
			[(_this select 2),(_this select 3)] call MBG_WOO_FUNC_OnMap_Waypoint;
		};
	};
};